﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace server
{
    public partial class Impostazioni : Form
    {
        private bool flag;
        private UInt16 Porta;
        private String path = Path.Combine(Environment.CurrentDirectory, @".\km_server.conf");
 
        private bool CtrlPort(String tmp)
        {
            if (!tmp.Equals(""))
            {
                try
                {
                    Porta = UInt16.Parse(tmp);
                    if (Porta < 1025 || Porta > 65535)
                    {
                        MessageBox.Show("Port number error!\nPlease insert a value between 1025 and 65535!");
                        porta.Text = "";
                        return false;
                    }
                }
                catch
                {
                    MessageBox.Show("Port number error!\nPlease insert a value between 1025 and 65535!");
                    porta.Text = "";
                    return false;
                }
                return true;
            }
            else
            {
                MessageBox.Show("Port number error!\nPlease insert a value between 1025 and 65535!");
                porta.Text = "";
                return false;
            }
        }

        public Impostazioni()
        {
            InitializeComponent();
        }
        public Impostazioni(UInt16 port, String un)
        {
            InitializeComponent();
            
            if (File.Exists(path))
            {
                 using (StreamReader LeggiConf = File.OpenText(path))
                    {
                        username.Text = LeggiConf.ReadLine();
                        porta.Text = LeggiConf.ReadLine();
                    }
            }
            else
            {
                using (StreamWriter ScriviConf = File.CreateText(path))
                {
                    ScriviConf.WriteLine(un);
                    ScriviConf.WriteLine(port.ToString());
                }
                username.Text = un;
                porta.Text = port.ToString();
            }
        }

  /*      private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
*/
        public bool getReady()
        {
            return false;
        }

        public string port
        {
            get { return porta.Text; }
        }

        public string user
        {
            get { return username.Text; }
        }
        public string pass
        {
            get { return pass1.Text; }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (username.Text.Equals(""))
            {
                flag = false;
                MessageBox.Show("Insert Username!");
            }

            if (CtrlPort(porta.Text))
            {
                UInt16.Parse(porta.Text);
                flag = true;
            }
            else
            {
                flag = false;
            }

            if (pass1.Text.Equals("") || pass2.Text.Equals(""))
            {
                flag = false;
                MessageBox.Show("Please insert Password!");
            }

            if(!(pass1.Text.Equals(pass2.Text))){
                flag=false;
                MessageBox.Show("Parameters unsetted!\nPasswords must be equals!");
            }
            if (flag)
            {
                this.DialogResult = DialogResult.OK;
                using (StreamWriter ScriviConf = File.CreateText(path))
                {
                    ScriviConf.WriteLine(username.Text);
                    ScriviConf.WriteLine(porta.Text);
                }
            }
            else
            {
                this.DialogResult = DialogResult.Abort;
            }
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Abort;
            this.Close();
        }

        private void Impostazioni_Load(object sender, EventArgs e)
        {

        }

  /*      private void Impostazioni_Load(object sender, EventArgs e)
        {

        }*/
    }
}
